﻿using System;
using System.Collections.Generic;
using System.IO;
using GeoAPI.Geometries;

namespace NetTopologySuite.GeometriesGraph
{
	public class Node : GraphComponent
	{
		public Node(Coordinate coord, EdgeEndStar edges)
		{
			this._coord = coord;
			this._edges = edges;
			base.Label = new Label(0, Location.Null);
		}

		public override Coordinate Coordinate
		{
			get
			{
				return this._coord;
			}
			protected set
			{
				this._coord = value;
			}
		}

		public EdgeEndStar Edges
		{
			get
			{
				return this._edges;
			}
			protected set
			{
				this._edges = value;
			}
		}

		public bool IsIncidentEdgeInResult()
		{
			IEnumerator<EdgeEnd> enumerator = this.Edges.Edges.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Edge.IsInResult)
					{
						return true;
					}
				}
			}
			return false;
		}

		public override bool IsIsolated
		{
			get
			{
				return base.Label.GeometryCount == 1;
			}
		}

		public override void ComputeIM(IntersectionMatrix im)
		{
		}

		public void Add(EdgeEnd e)
		{
			this._edges.Insert(e);
			e.Node = this;
		}

		public void MergeLabel(Node n)
		{
			this.MergeLabel(n.Label);
		}

		public void MergeLabel(Label label2)
		{
			for (int i = 0; i < 2; i++)
			{
				Location location = this.ComputeMergedLocation(label2, i);
				if (base.Label.GetLocation(i) == Location.Null)
				{
					base.Label.SetLocation(i, location);
				}
			}
		}

		public void SetLabel(int argIndex, Location onLocation)
		{
			if (base.Label == null)
			{
				base.Label = new Label(argIndex, onLocation);
				return;
			}
			base.Label.SetLocation(argIndex, onLocation);
		}

		public void SetLabelBoundary(int argIndex)
		{
			if (base.Label == null)
			{
				return;
			}
			Location location = Location.Null;
			if (base.Label != null)
			{
				location = base.Label.GetLocation(argIndex);
			}
			Location location2;
			if (location != Location.Interior)
			{
				if (location == Location.Boundary)
				{
					location2 = Location.Interior;
				}
				else
				{
					location2 = Location.Boundary;
				}
			}
			else
			{
				location2 = Location.Boundary;
			}
			base.Label.SetLocation(argIndex, location2);
		}

		public Location ComputeMergedLocation(Label label2, int eltIndex)
		{
			Location location = base.Label.GetLocation(eltIndex);
			if (!label2.IsNull(eltIndex))
			{
				Location location2 = label2.GetLocation(eltIndex);
				if (location != Location.Boundary)
				{
					location = location2;
				}
			}
			return location;
		}

		public void Write(TextWriter outstream)
		{
			outstream.WriteLine(string.Concat(new object[]
			{
				"node ",
				this._coord,
				" lbl: ",
				base.Label
			}));
		}

		public override string ToString()
		{
			return this._coord + " " + this._edges;
		}

		private Coordinate _coord;

		private EdgeEndStar _edges;
	}
}
